#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    int romanToInt(string s) {
        unordered_map<string, int> m = unordered_map<string, int>({{"I",  1},
                                                                   {"IV", 4},
                                                                   {"V",  5},
                                                                   {"IX", 9},
                                                                   {"X",  10},
                                                                   {"IL", 40},
                                                                   {"L",  50},
                                                                   {"XC", 90},
                                                                   {"C",  100},
                                                                   {"CD", 400},
                                                                   {"D",  500},
                                                                   {"CM", 900},
                                                                   {"M",  1000}});
        int res = 0;
        for (int i = 0; i < s.size();) {
            if (i == s.size() - 1) {
                res += m[string(1, s[i])];
                i++;
            } else {
                string src = s.substr(i, 2);
                if (m.find(src) != m.end()) {
                    res += m[src];
                    i += 2;
                } else {
                    res += m[string(1, s[i])];
                    i++;
                }
            }
        }
        return res;
    }
};

int main() {

    Solution solution;
    cout << solution.romanToInt("LVIII") << "\n";
    return 0;
}
